//
//  YKhomepageViewController.m
//  Jwy-ijkplayerDemo
//
//  Created by XiaoMing on 16/8/11.
//  Copyright © 2016年 JWY. All rights reserved.
//

#import "YKhomepageViewController.h"
#import "ListTableViewCell.h"
#import "NetWorkEngine.h"
#import "listModel.h"
#import "ODRefreshControl.h"
#import "PlayViewController.h"
// 映客接口
#define HomeData [NSString stringWithFormat:@"http://service.ingkee.com/api/live/gettop?imsi=&uid=17800399&proto=5&idfa=A1205EB8-0C9A-4131-A2A2-27B9A1E06622&lc=0000000000000026&cc=TG0001&imei=&sid=20i0a3GAvc8ykfClKMAen8WNeIBKrUwgdG9whVJ0ljXi1Af8hQci3&cv=IK3.1.00_Iphone&devi=bcb94097c7a3f3314be284c8a5be2aaeae66d6ab&conn=Wifi&ua=iPhone&idfv=DEBAD23B-7C6A-4251-B8AF-A95910B778B7&osversion=ios_9.300000&count=5&multiaddr=1"]
#define IphoneWidth [UIScreen mainScreen].bounds.size.width
#define IphoneHeight [UIScreen mainScreen].bounds.size.height
@interface YKhomepageViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *listTableView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@end

@implementation YKhomepageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatNav];
    [self creatUI];
}
-(void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [super viewWillAppear:animated];
    [self newData];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [super viewWillAppear:animated];
}
-(void)creatNav{
    self.view.backgroundColor=[UIColor whiteColor];
    self.navigationItem.title =@"Gwanaak-映客直播";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.navigationController.navigationBar.translucent = NO;
    
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"HK" style:UIBarButtonItemStyleDone target:self action:@selector(dismissVC)];
    
}
-(void)dismissVC{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)creatUI{
    _listTableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [_listTableView registerNib:[UINib nibWithNibName:@"ListTableViewCell" bundle:nil] forCellReuseIdentifier:@"listCell"];
    _listTableView.dataSource=self;
    _listTableView.delegate=self;
    _listTableView.rowHeight = 520;
    _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_listTableView];
    
    ODRefreshControl *refreshControl = [[ODRefreshControl alloc] initInScrollView:self.listTableView];
    [refreshControl addTarget:self action:@selector(dropViewDidBeginRefreshing:) forControlEvents:UIControlEventValueChanged];

}
- (void)dropViewDidBeginRefreshing:(ODRefreshControl *)refreshControl
{
   dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [refreshControl endRefreshing];
        [self newData];
    });
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"listCell"];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    listModel * model = [self.dataArray objectAtIndex:indexPath.row];
    cell.model = model;
    
    return cell;
    
    
}
// cell动画
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //设置Cell的动画效果为3D效果
    //设置x和y的初始值为0.1；
    cell.layer.transform = CATransform3DMakeScale(0.1, 0.1, 1);
    //x和y的最终值为1
//    [UIView animateWithDuration:1 animations:^{
//        cell.layer.transform = CATransform3DMakeScale(1, 1, 1);
//    }];
    [UIView animateWithDuration:1 animations:^{
        cell.layer.transform = CATransform3DMakeScale(1.2, 1.2, 1);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1 animations:^{
            cell.layer.transform = CATransform3DMakeScale(1, 1, 1);
        }];
    }];
}
// Cell跳转直播
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PlayViewController * playVC = [[PlayViewController alloc] init];
    listModel * model = self.dataArray[indexPath.row];
    // 直播url
    playVC.playUrl = model.url;
    // 直播图片
    playVC.imageUrl = model.portrait;
    [self.navigationController pushViewController:playVC animated:YES];
    self.navigationController.navigationBar.hidden = YES;
    
}
-(NSMutableArray *)dataArray{
    if (_dataArray==nil) {
        _dataArray=[[NSMutableArray alloc]init];
    }
    return _dataArray;
}
// Get数据
- (void)newData
{
    [self.dataArray removeAllObjects];
    __weak YKhomepageViewController *weakSelf = self;
    NetWorkEngine * netWork = [[NetWorkEngine alloc] init];
    [netWork AfJSONGetRequest:HomeData];
    netWork.successfulBlock = ^(id object){
        NSArray *listArray = [object objectForKey:@"lives"];
        
        for (NSDictionary *dic in listArray) {
            
            listModel * model = [[listModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            model.city = dic[@"city"];
            model.portrait = dic[@"creator"][@"portrait"];
            model.name = dic[@"creator"][@"nick"];
            model.online_users =dic[@"online_users"];
            model.url = dic[@"stream_addr"];
            [weakSelf.dataArray addObject:model];
            
        }
        [self.listTableView reloadData];
         //NSLog(@"%@",object);
    };
    
}
// 导航栏隐藏
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    //NSLog(@"offset---scroll:%f",self.listTableView.contentOffset.y);
    
    //scrollView已经有拖拽手势，直接拿到scrollView的拖拽手势
    UIPanGestureRecognizer* pan = scrollView.panGestureRecognizer;
    //获取到拖拽的速度 >0 向下拖动 <0 向上拖动
    CGFloat velocity = [pan velocityInView:scrollView].y;
    
    if (velocity<-5) {
        
        //向上拖动，隐藏导航栏
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
    else if (velocity>5) {
        //向下拖动，显示导航栏
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }
}

@end
